#ifndef PEER_H
#define PEER_H

#include "common_types.h"
#include "i_io_handler.h"
#include "i_package_sender.h"
#include "i_package_codec.h"

#include <boost/asio/ip/udp.hpp>
#include <boost/enable_shared_from_this.hpp>

using boost::asio::ip::udp;

class UPeerImp;
class UPeer : public IIoHandler, public boost::enable_shared_from_this<UPeer>
{	
	boost::shared_ptr<UPeerImp> imp_;
public:
	enum ShutdownReason
	{
		INPURPOSE,
		RESETED,
		OUTTIME
	};

	typedef boost::shared_ptr<UPeer> Ptr;

	UPeer(const udp::endpoint& remoteAddr, IPackageSender::Ptr packageSender);
	virtual ~UPeer();

	void close(const ShutdownReason& reason) ;
	virtual void close();
	virtual void start() ;
	virtual void send(IPackage::Ptr package);
	virtual void send(IPackage* package);
	virtual void sendConnectingPackage();
	virtual bool isDisposed() const;
	bool isEstablished() const;
	void onConnectingReceived(bool connectingSide = false);
	void onReset(bool byremote = false);
	const udp::endpoint getRemoteAddr() const ;
	void confirmPackage(unsigned int packageSeq);
	bool onPackageReceived(IPackage::Ptr package, unsigned int packageSeq);
};

#endif
